import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public abstract class ViewHelper<T> {
    public FXMLLoader loader=new FXMLLoader();
    Logger logger= LoggerFactory.getLogger(this.getClass());
    public Scene scene;
    public T controller;
    public void init(){

        loader.setLocation(this.getClass().getResource(getFXMLName()));
        try {
            controller=loader.getController();
            scene=new Scene(loader.load());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    final String getFXMLName(){
        String resourceAndLowerCase = getResourceAndLowerCase(".fxml");
        return resourceAndLowerCase;
    }
    String getResourceAndLowerCase(String ending){
        final String clazzWithending=this.getClass().getSimpleName();
        if(clazzWithending.endsWith("View")){
            int index = clazzWithending.lastIndexOf("View");
            String substring = clazzWithending.substring(0, index);
            return substring+ending;
        }else{
            logger.error("getResourceAndLowerCase error");
            return null;
        }
    }
}
